package com.yenlien.traceyun.usercenter.service;

import com.yenlien.traceyun.usercenter.dao.CompanyDao;
import com.yenlien.traceyun.usercenter.dao.CompanyExaminedDao;
import com.yenlien.traceyun.usercenter.entity.Company;
import com.yenlien.traceyun.usercenter.entity.CompanyExamined;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2021-02-19
 * @description:
 **/

@Service
public class CompanyExaminedService {

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private CompanyExaminedDao companyExaminedDao;

    /**
     * 检验
     *
     * @param examined
     */

    @Transactional(rollbackFor = RuntimeException.class)
    public void examined(CompanyExamined examined) {
        Company company = companyDao.findById(examined.getCompanyId());
        if (company.getExamined() != null && company.getExamined().equals("1")) {
            throw new BusinessException("500", "已审核过了");
        }
        examined.setExaminedTime(new Date());
        companyExaminedDao.save(examined);
        company.setExamined(examined.isPassed() ? "1" : "0");
        companyDao.save(company);
    }

    public List<CompanyExamined> list(String companyId) {
        return companyExaminedDao.findByCompanyId(companyId, Sort.by(Sort.Direction.DESC, "examinedTime"));
    }
}
